package com.woniuxy.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Order;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.util.RabbitmqConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author dingpf
 * @version 1.0
 * @description
 * @create 2025/5/6 14:40
 *
 * 1：获取消息
 * 2：做业务逻辑
 *    判断用户的订单状态
 *    1：未付款状态，就要取消订单
 *    2：已付款，什么都不做
 *
 * 3：确认消息
 */
@Component
public class CheckOrderStatsConsumer {

    @Resource
    private OrderMapper orderMapper;

    @RabbitListener(queues = RabbitmqConst.CHECKORDERSTATUS_QUEUE)
    public void receive(String msg, Channel channel, Message message){
        try {
            Order order = orderMapper.queryOrderInfoByOrderNum(msg);
            if (order.getStatus().equals(1)){
                //把订单状态改成已取消
                Order order1 = new Order();
                order1.setOrderNum(msg);
                order1.setStatus(4);
                orderMapper.updateOrderStatus(order1);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
